$(function () {
    //登录和注册的切换
    $('#goto-register').on('click', function () {
        $('#register').stop().show()
    })
    $('#goto-login').on('click', function () {
        $('#register').stop().hide()
    })

    //表单校验


    //注册表单校验事件
    const base = 'http://api-breakingnews-web.itheima.net'



    $('#register .layui-form').on('submit', async function (e) {
        e.preventDefault()
        let obj = {
            username: $('#register input[name=username]').val(),
            password: $('#register input[name=password]').val()
        }
        handleDataFormat(obj)
        const res = await reqPostRegister(obj)
        const { data: { status, message } } = res

        $('#register').stop().hide()
    })

    //登录校验
    $('#login .layui-form').on('submit', async function (e) {
        e.preventDefault()
        let obj = {
            username: $('#login input[name=username]').val(),
            password: $('#login input[name=password]').val()
        }
        const res = await reqPostLogin(obj)
        // console.log(res)
        // 将token存储在本地, 下次调用别的接口用token
        localStorage.setItem('big-event-token', res.data.token)
        // location.href = './index.html'
        // 跳转到index页面
        if (res.data.status === 0) {
            location.href = '/index.html'
        }

    })
})

